package com.ssy.lingxi.purchase.controller.tender;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.purchase.api.dto.request.tender.*;
import com.ssy.lingxi.purchase.api.dto.response.tender.ExpertExtractDetailResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.ExpertExtractInviteTenderResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.ExpertExtractResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.InviteTenderResponse;
import com.ssy.lingxi.purchase.api.enums.tender.ExpertExtractStatusEnum;
import com.ssy.lingxi.purchase.api.enums.tender.InviteTenderOutStatusEnum;
import com.ssy.lingxi.purchase.entity.tender.ExpertExtract;
import com.ssy.lingxi.purchase.entity.tender.ExpertExtractRecord;
import com.ssy.lingxi.purchase.entity.tender.InviteTender;
import com.ssy.lingxi.purchase.service.tender.IExpertExtractService;
import com.ssy.lingxi.purchase.service.tender.IInviteTenderService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu: 招投标-专家抽取管理
 * 专家抽取管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/3/20
 */
@RestController
@RequestMapping("/purchase/expertExtract")
public class ExpertExtractController extends BaseController {

    @Resource
    private IExpertExtractService expertExtractService;

    @Resource
    private IInviteTenderService inviteTenderService;

    /**
     * 查询专家抽取管理列表
     * @param pageVO 分页实体
     * @param expertExtractQueryRequest 参数
     */
    @RequestMapping(value = "/getExpertExtractList", method = RequestMethod.GET)
    public Wrapper<PageData<ExpertExtractResponse>> getExpertExtractList(PageVO pageVO, ExpertExtractQueryRequest expertExtractQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<ExpertExtract> result = expertExtractService.getExpertExtractList(sysUser, pageVO, expertExtractQueryRequest);
        List<ExpertExtractResponse> resultList = result.getContent().stream().map(expertExtract -> {
            List<ExpertExtractRecord> expertExtractRecordList = expertExtract.getExpertExtractRecordList();
            long count = expertExtractRecordList.stream().filter(expertExtractRecord -> (expertExtractRecord.getStatus().equals(ExpertExtractStatusEnum.Confirm.getCode()) || expertExtractRecord.getStatus().equals(ExpertExtractStatusEnum.Evaluation.getCode()))).count();
            ExpertExtractResponse expertExtractResponse = BeanUtil.copyProperties(expertExtract, ExpertExtractResponse.class);
            expertExtractResponse.setExpertCount(expertExtractRecordList.size());
            expertExtractResponse.setExpertConfirmCount((int) count);
            expertExtractResponse.setStatus(expertExtract.getStatus());
            //转换外部状态
            Integer inviteTenderOutStatus = expertExtract.getInviteTender().getInviteTenderOutStatus();
            ExpertExtractInviteTenderResponse inviteTenderResponse = expertExtractResponse.getInviteTender();
            inviteTenderResponse.setInviteTenderOutStatusValue(InviteTenderOutStatusEnum.getMessage(inviteTenderOutStatus));
            expertExtractResponse.setInviteTender(inviteTenderResponse);
            return expertExtractResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询专家抽取信息
     * @param id 专家抽取id
     */
    @RequestMapping(value = "/getExpertExtract", method = RequestMethod.GET)
    public Wrapper<ExpertExtractDetailResponse> getExpertExtract(@RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        ExpertExtract expertExtract = expertExtractService.getExpertExtract(sysUser, id);
        if(expertExtract != null){
            return Wrapper.success(this.modelMapper.map(expertExtract, ExpertExtractDetailResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 查询招标列表
     */
    @RequestMapping(value = "/getInviteTenderList", method = RequestMethod.POST)
    public Wrapper<PageData<InviteTenderResponse>> getInviteTenderList(@RequestBody ExpertExtractInviteTenderQueryRequest expertExtractInviteTenderQuery) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        //待评标状态
        InviteTenderQueryRequest inviteTenderQueryRequest = BeanUtil.copyProperties(expertExtractInviteTenderQuery, InviteTenderQueryRequest.class);
        List<Integer> inviteTenderOutStatusList = new ArrayList<>();
        inviteTenderOutStatusList.add(InviteTenderOutStatusEnum.Not_Selection_Tender.getCode());
        inviteTenderQueryRequest.setInviteTenderOutStatusList(inviteTenderOutStatusList);
        //当前时间小于评标截止时间
        inviteTenderQueryRequest.setLeEvaluationEndTime(true);
        Page<InviteTender> result = inviteTenderService.getInviteTenderList(sysUser, inviteTenderQueryRequest);
        List<InviteTenderResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<InviteTenderResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 新增/修改专家抽取
     * @param expertExtractRequest 参数
     */
    @RequestMapping(value = "/saveOrUpdateExpertExtract", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateExpertExtract(@RequestBody ExpertExtractRequest expertExtractRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(expertExtractService.saveOrUpdateExpertExtract(sysUser, this.modelMapper.map(expertExtractRequest, ExpertExtract.class)));
    }

    /**
     * 删除
     * @param simpleIdRequest id
     */
    @RequestMapping(value = "/deleteExpertExtract", method = RequestMethod.POST)
    public Wrapper<Boolean> deleteExpertExtract(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(expertExtractService.deleteExpertExtract(sysUser, simpleIdRequest));
    }

    /**
     * 发送抽取通知
     * @param simpleIdRequest 评标专家库实体
     */
    @RequestMapping(value = "/sendExpertExtract", method = RequestMethod.POST)
    public Wrapper<Boolean> sendExpertExtract(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(expertExtractService.sendExpertExtract(sysUser, simpleIdRequest));
    }

    /**
     * 再次发送抽取通知
     * @param simpleIdRequest 评标专家库实体
     */
    @RequestMapping(value = "/sendExpertExtractAgain", method = RequestMethod.POST)
    public Wrapper<Boolean> sendExpertExtractAgain(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(expertExtractService.sendExpertExtractAgain(sysUser, simpleIdRequest));
    }
}
